import ws from 'ws';
export const clients: any = {}
const wss = new ws.Server({ port: 28797 })
wss.on('connection', (ws: ws) => {
    console.log('Client connected');
    ws.on('message', (event: any) => {
        const message = JSON.parse((event).toString('utf-8'));

        if (message.type == 'identification') {
            clients[message.data] = ws;
        } else if (message.type == 'ws') {
            if (clients.panel) {
                clients.panel.send(message);
            }
        }
    });
    ws.on('close', () => {
        console.log('Client disconnected');
        removeClient(ws);
    });
})
const removeClient = (ws: ws) => {
    for (const key in clients) {
        if (clients[key] === ws) {
            delete clients[key];
            console.log(`Removed client: ${key}`);
            break;
        }
    }
};
